#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h>

#define SLAM_FILE_PATH "/home/xinu/slam.txt"

static char wbuf[] = "Hello slam-xinu";
static char rbuf[128];

static int vfs_operate_init(void)
{
	struct file * fp;
	mm_segment_t cur_mm_seg;
	loff_t fpos = 0;

	printk("<xinu>in %s!\n",__func__);

	fp = filp_open(SLAM_FILE_PATH, O_RDWR | O_CREAT, 0644);
	if (IS_ERR(fp)) {
		printk("<xinu>filp_open error\n");
		return -1;
	}

	cur_mm_seg = get_fs();
	set_fs(KERNEL_DS);
	vfs_write(fp, wbuf, sizeof(wbuf), &fpos);
	fpos = 0;
	vfs_read(fp, rbuf, sizeof(rbuf), &fpos);
	printk("<xinu>read content: %s\n", rbuf);
	set_fs(cur_mm_seg);

	filp_close(fp, NULL);

	return 0;
}

static void vfs_operate_exit(void)
{
	printk("Bye %s!\n", __func__);
}

module_init(vfs_operate_init);
module_exit(vfs_operate_exit);

MODULE_LICENSE("GPL");
